﻿namespace Jsl.FxCop
{
    using System;
    using Microsoft.VisualStudio.CodeAnalysis.Extensibility;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class When_checking_for_String_ToString
    {
        [TestMethod]
        public void ToString_against_a_string_produces_problem()
        {
            new FixSimplePerformanceChanges()
                .RunMethodRule(
                    () =>
                    {
                        string x = "123";
                        string y = x.ToString();
                        string z = y;           // Needed so that the y variable doesn't get optimized out.
                    })
                .HasProblem(
                    100,
                    FixCategories.NonBreaking,
                    MessageLevel.Warning,
                    "Remove the call to ToString since the object is already a string.")
                .HasNoMoreProblems();
        }

        [TestMethod]
        public void ToString_not_against_a_string_produces_no_problems()
        {
            new FixSimplePerformanceChanges()
                .RunMethodRule(
                    () =>
                    {
                        string x = this.ToString();
                        string y = x;           // Needed so that the x variable doesn't get optimized out.
                    })
                .HasNoMoreProblems();
        }

        [TestMethod]
        public void No_problem_if_ToString_has_parameters()
        {
            new FixSimplePerformanceChanges()
                .RunMethodRule(
                    () =>
                    {
                        string x = "123";
                        string y = x.ToString((IFormatProvider)null);
                        string z = y;           // Needed so that the y variable doesn't get optimized out.
                    })
                .HasNoMoreProblems();
        }
    }

    [TestClass]
    public class When_creating_new_EventArgs
    {
        [TestMethod]
        public void Using_EventArgs_Empty_is_not_a_problem()
        {
            new FixSimplePerformanceChanges()
                .RunMethodRule(() => EventArgs.Empty)
                .HasNoMoreProblems();
        }

        [TestMethod]
        public void Creating_a_new_EventArg_is_a_problem()
        {
            new FixSimplePerformanceChanges()
                .RunMethodRule(() => new EventArgs())
                .HasProblem(
                    100,
                    FixCategories.NonBreaking,
                    MessageLevel.Warning,
                    "Use EventArgs.Empty instead of creating a new EventArgs instance.");
        }
    }
}